package com.gcxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gcxy.dto.student.AddStudentInfoDTO;
import com.gcxy.entity.Semester;
import com.gcxy.entity.User;
import com.gcxy.mapper.StudentTeacherMapper;
import com.gcxy.service.SemesterService;
import com.gcxy.service.StudentService;
import com.gcxy.service.StudentTeacherService;
import com.gcxy.entity.StudentTeacher;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 学生老师关联表:表示从第几周开始某学生的指导教师是谁 服务实现类
 * </p>
 *
 * @author Huang先森
 * @since 2024-07-22
 */
@Service
public class StudentTeacherServiceImpl extends ServiceImpl<StudentTeacherMapper, StudentTeacher> implements StudentTeacherService {

    @Resource
    private SemesterService semesterService;

    @Autowired
    private StudentService studentService;

    /**
     * 获取当前教师学生关联关系
     *
     * @param studentId
     * @param semesterId
     * @return
     */
    @Override
    public StudentTeacher getByStudentId(Integer studentId, Integer semesterId) {
        if (null == semesterId) {
            studentId = semesterService.getCurrentSemester().getId();
        }
        LambdaQueryWrapper<StudentTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentTeacher::getStudentId, studentId)
                .eq(StudentTeacher::getSemesterId, semesterId);
        return getOne(queryWrapper);
    }

    /**
     * 获取当前学期实际指导教师
     *
     * @param semesterId
     * @return
     */
    @Override
    public List<StudentTeacher> getTeacherBySemesterId(Integer semesterId) {
        LambdaQueryWrapper<StudentTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentTeacher::getSemesterId, semesterId);
        return list(queryWrapper);
    }

    @Override
    public boolean insertStudentTeacher(AddStudentInfoDTO addStudentInfoDTO, User user) {
        StudentTeacher studentTeacher = new StudentTeacher();
        // 绑定学期
        Semester semester = semesterService.getCurrentSemester();
        if (addStudentInfoDTO.getSemesterId() == null) {
            if (ObjectUtils.isEmpty(semester)) {
                throw new RuntimeException("学期绑定失败，学期中未创建新的学期!");
            } else {
                studentTeacher.setSemesterId(semester.getId());
            }
        } else {
            studentTeacher.setSemesterId(addStudentInfoDTO.getSemesterId());
        }
        studentTeacher.setStudentId(user.getId());
        studentTeacher.setHonoraryTeacherId(addStudentInfoDTO.getHonoraryTeacherId());
        studentTeacher.setPracticalTeacherId(addStudentInfoDTO.getPracticalTeacherId());

        LambdaQueryWrapper<StudentTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentTeacher::getStudentId, user.getId()).eq(StudentTeacher::getSemesterId, studentTeacher.getSemesterId());
        if (null != getOne(queryWrapper)) {
            studentTeacher.setId(getOne(queryWrapper).getId());
            return updateById(studentTeacher);
        }

        return save(studentTeacher);
    }

    @Override
    public List<StudentTeacher> getAllByStudentId(Integer studentId) {
        LambdaQueryWrapper<StudentTeacher> studentTeacherLambdaQueryWrapper = new LambdaQueryWrapper<StudentTeacher>()
                .eq(StudentTeacher::getStudentId, studentId);
        return list(studentTeacherLambdaQueryWrapper);
    }

    @Override
    public boolean updateStudentTeacher(StudentTeacher studentTeacher) {
        return updateById(studentTeacher);
    }

    @Override
    public boolean deleteByStudentId(Integer studentId, Integer semesterId) {
        if (null == studentId) {
            studentId = semesterService.getCurrentSemester().getId();
        }
        LambdaQueryWrapper<StudentTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentTeacher::getStudentId, studentId)
                .eq(StudentTeacher::getSemesterId, semesterId);

        return remove(queryWrapper);
    }

    /**
     * 根据学期id获取组织下当前学期对应的学生信息
     *
     * @param semesterId
     * @return
     */
    @Override
    public List<StudentTeacher> getStudentBySemesterId(Integer semesterId, Integer agencyId) {
        //获取当前组织下的学生信息
        List<User> studentList = studentService.getStudentListByAgencyId(agencyId);
        if (CollectionUtils.isEmpty(studentList)) {
            return new ArrayList<>();
        }
        List<Integer> studentIds = studentList.stream().map(User::getId).collect(Collectors.toList());
        LambdaQueryWrapper<StudentTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(StudentTeacher::getStudentId, studentIds)
                .eq(StudentTeacher::getSemesterId, semesterId);
        return list(queryWrapper);
    }

    /**
     * 获取绑定学生的名誉教师信息
     *
     * @param honoraryTeachers
     * @return
     */
    @Override
    public List<StudentTeacher> getHonoraryTeachers(List<User> honoraryTeachers) {
        if (CollectionUtils.isEmpty(honoraryTeachers)) {
            return new ArrayList<>();
        }
        List<Integer> honoraryTeacherIds = honoraryTeachers.stream().map(User::getId).collect(Collectors.toList());
        LambdaQueryWrapper<StudentTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(StudentTeacher::getHonoraryTeacherId, honoraryTeacherIds);
        return list(queryWrapper);
    }
}
